package com.novel.controller;

import com.novel.annotation.ResponseResult;
import com.novel.constants.ResultEnum;
import com.novel.entiy.dto.LoginDTO;
import com.novel.entiy.dto.RegisterDTO;
import com.novel.entiy.TpUser;
import com.novel.entiy.dto.RewardDTO;
import com.novel.entiy.vo.UserInfo;
import com.novel.exception.BusinessException;
import com.novel.service.UserService;
import com.novel.utils.CommonUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author chihuo
 * @date 2022/4/5
 */
@ResponseResult
@RequestMapping("user")
@RestController
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping(value = "/getUserInfo")
    public TpUser getUserInfo() {
        return userService.getUserInfo();
    }

    @ApiOperation("修改个人信息")
    @PostMapping("/editUser")
    public void editUser(@RequestBody TpUser tpUser) {
        userService.editUser(tpUser);
    }

    @PostMapping(value = "/login")
    public UserInfo login(@Validated LoginDTO loginDTO, HttpServletRequest httpServletRequest, BindingResult bindingResult) {
        CommonUtil.paramValid(bindingResult);
        return userService.login(loginDTO);
    }

    @PostMapping(value = "/register")
    public void register(@Validated RegisterDTO registerDTO, BindingResult bindingResult) {
        CommonUtil.paramValid(bindingResult);
        userService.register(registerDTO);
    }

    @GetMapping(value = "/getEmailCode")
    public void getEmailCode(@RequestParam String email) {
        userService.getEmailCode(email);
    }

    @RequestMapping(value = "/failed")
    public void failed() {
        throw new BusinessException(ResultEnum.AUTH_FAIL);
    }

    @ApiOperation("充值 1元:100金币")
    @GetMapping(value = "/recharge/{amount}")
    public void recharge(@PathVariable Integer amount) {
        userService.recharge(amount);
    }

    @ApiOperation("退出登录")
    @GetMapping(value = "/logout")
    public void logout() {
        userService.logout();
    }

    @ApiOperation("申请成为作者")
    @GetMapping("/applyAuthor")
    public void applyAuthor() {
        userService.applyAuthor();
    }



    @ApiOperation("申请签约")
    @GetMapping("/applySign")
    public void applySign() {
        userService.applySign();
    }

    @ApiOperation("打赏小说")
    @PostMapping("/reward")
    public void reward(@RequestBody RewardDTO rewardDTO) {
        userService.reward(rewardDTO);
    }

}
